
package com.yanxin.workflow.service;


import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.builder.SubProcessBuilder;
import org.camunda.bpm.model.bpmn.instance.SubProcess;
import org.camunda.bpm.spring.boot.starter.event.PostDeployEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @program spring-cloud-example
 * @description:
 * @author: LiuYanXin
 * @create: 2022-01-20 00:11
 */

@Service
public class WorkflowService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowService.class);

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;

    @Autowired
    IdentityService identityService;

    @Autowired
    TaskService taskService;

    /*TaskService taskService = processEngine.getTaskService();
    IdentityService identityService = processEngine.getIdentityService();
    FormService formService = processEngine.getFormService();
    HistoryService historyService = processEngine.getHistoryService();
    ManagementService managementService = processEngine.getManagementService();
    FilterService filterService = processEngine.getFilterService();
    ExternalTaskService externalTaskService = processEngine.getExternalTaskService();
    CaseService caseService = processEngine.getCaseService();
    DecisionService decisionService = processEngine.getDecisionService();*/

    @Bean
    public JavaDelegate sayHelloDelegate() {
        return execution -> logger.info("Hello!");
    }

    @Bean
    public JavaDelegate doActionDelegate() {
        return execution -> {
            logger.info("Doing action {}", execution.getVariable("action"));
        };
    }

    @EventListener
    public void notify(final PostDeployEvent unused) {
        final ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("Sample").singleResult();
        logger.info("Found deployed process: {}", processDefinition);
        // Assert.notNull(processDefinition, "process 'Sample' should be deployed!");
    }

    @EventListener
    private void processPostDeploy(PostDeployEvent event) {
        runtimeService.startProcessInstanceByKey("Sample");
    }

    public void subProcessCreate(){

        BpmnModelInstance bpmn = Bpmn.createProcess().id("")
                .executable()
                .startEvent()
                .userTask()
                .subProcess("rpa_process")
                .embeddedSubProcess()
                .startEvent("sub_start")
                .userTask().endEvent()
                .subProcessDone().endEvent().done();

        SubProcess subProcess = bpmn.getModelElementById("rpa_process");
        subProcess.builder()
                .multiInstance()
                .camundaCollection("myCollection")
                .camundaElementVariable("myVar")
                .multiInstanceDone();


    }

}
